package vip.liux.front.infrastructure.annotations;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;

import java.lang.annotation.Annotation;

@Configuration(proxyBeanMethods = false)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class ProxyAuditedConfiguration extends AbstractAuditedConfiguration {

    @Bean(name = "internalAuditAnnotationProcessor")
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public AuditedAnnotationBeanPostProcessor auditedAdvisor() {
        Assert.state(this.enableAudited != null, "@EnableAudited annotation metadata was not injected");
        AuditedAnnotationBeanPostProcessor bpp = new AuditedAnnotationBeanPostProcessor();
        bpp.configure(this.auditingStore, this.ambientScopeProvider);
        Class<? extends Annotation> customAuditedAnnotation = this.enableAudited.getClass("annotation");
        if (customAuditedAnnotation != AnnotationUtils.getDefaultValue(EnableAudited.class, "annotation")) {
            bpp.setAuditedAnnotationType(customAuditedAnnotation);
        }
        bpp.setProxyTargetClass(this.enableAudited.getBoolean("proxyTargetClass"));
        bpp.setOrder(this.enableAudited.<Integer>getNumber("order"));
        return bpp;
    }
}
